package com.lanhai.javaBean;

import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.model.FreeArea;
import com.lanhai.utils.RegionUtil;
import com.lanhai.utils.StringUtil;
import com.mybatis.dao.FreeAreaDao;

public class FreeAreaBean {

	/**
	 * 加载自由区列表
	 */
	public static List<FreeArea> loadData(SqlSessionFactory sqlSessionFactory){
		List<FreeArea> freeAreaList = new ArrayList<FreeArea>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			FreeAreaDao faDao = sqlSession.getMapper(FreeAreaDao.class);
			List<Map<String, Object>> list = faDao.selectAll();
			if(list.size() > 0){
				for(Map<String, Object> m:list){
					FreeArea fa = new FreeArea();
					fa.setId((Integer)m.get("id"));
					fa.setName((String)m.get("name"));
					fa.setIpAddress((String)m.get("ipAddress"));//IP地址
					String xStr = (String)m.get("mapXArr");
					boolean flag = false;
					if(xStr != null){
						Double[] mapXArr = StringUtil.convertDoubleArray(xStr);
						fa.setMapXArr(mapXArr);
						flag = true;
					}
					String yStr = (String)m.get("mapYArr");
					if(yStr != null){
						Double[] mapYArr = StringUtil.convertDoubleArray(yStr);
						fa.setMapYArr(mapYArr);
					}
					if(flag){
						GeneralPath shape = RegionUtil.getCtrlShape(fa.getMapXArr(), fa.getMapYArr());
						fa.setShape(shape);
					}
					freeAreaList.add(fa);					
				}
			}
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	sqlSession.close();
	    }
        return freeAreaList;
	}
	
}
